﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Net.Mail;
using System.Text;

namespace GabageFactory.Models
{
    public class UserReposity : Reposity
    {
        public IEnumerable<User> ListUsers(long factoryId)
        {
            var users = from u in _dataContext.Users
                        where (factoryId > 0 ? u.FactoryId == factoryId : 1 == 1)
                        select u;
            foreach (User u in users)
            {
                yield return u;
            }
        }

        public IEnumerable<User> QueryUsers(string name, int roleId, int factoryId)
        {
            name = !string.IsNullOrEmpty(name) ? name : "";
            var users = from u in _dataContext.Users
                        where (
                        u.Name.Contains(name) &&
                        (roleId > 0 ? u.RoleId == roleId : 1 == 1) && 
                        (factoryId > 0 ? u.FactoryId == factoryId : 1 == 1)
                        )
                        select u;
            foreach (User u in users)
            {
                yield return u;
            }
        }

        public User GetUserByName(string userName)
        {
            var user = from u in _dataContext.Users
                       where userName == u.Name
                       select u;

            if (user.Count() > 0)
            {
                return user.First();
            }
            else return null;
        }

        public User GetUserById(long id)
        {
            var user = from u in _dataContext.Users
                       where id == u.Id
                       select u;

            if (user.Count() > 0)
            {
                return user.First();
            }
            else return null; ;
        }

        public void Login(int factoryId, int userId)
        {
            _dataContext.sp_Calculate_Miss_Data_On_Login(factoryId, userId);
        }

        public User CreateUser(User user)
        {
            user.CreateTime = DateTime.Now;
            user.ModifyTime = DateTime.Now;
            _dataContext.Users.InsertOnSubmit(user);
            SubmitChanges();
            return user;
        }

        public bool DeleteUser(User user)
        {
            _dataContext.Users.DeleteOnSubmit(user);
            SubmitChanges();
            return true;
        }

        public User EditUser(User user)
        {
            SubmitChanges();
            return user;
        }

        public void SendPwdEmail(string to, string name, string password)
        {
            MailMessage message = new MailMessage();
            message.To.Add(new MailAddress(to));
            message.Subject = "密码重置通知";
            message.IsBodyHtml = true;
            message.Body = string.Format("亲爱的{0}，你的密码已经被管理员重置，临时密码是{1}，请登录系统重新设置密码。", name, password);

            try
            {
                SmtpClient client = new SmtpClient();
                client.Send(message);
            }
            catch
            {
                //empty
            }
        }
    }
}